/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

/**
 *
 * @author hxg
 */
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class MapAdapter<K, V> extends XmlAdapter<MapElements[], Map<K, V>> {

    @Override
    public MapElements[] marshal(Map<K, V> arg0) throws Exception {
        MapElements[] mapElements = new MapElements[arg0.size()];
        int i = 0;
        for (Map.Entry<K, V> entry : arg0.entrySet()) {
            mapElements[i++] = new MapElements(entry.getKey(), entry.getValue());
        }

        return mapElements;
    }

    @Override
    public Map<K, V> unmarshal(MapElements[] arg0) throws Exception {
        Map<K, V> r = new HashMap();
        for (MapElements<K, V> mapelement : arg0) {
            r.put(mapelement.key, mapelement.value);
        }
        return r;
    }
}
